package com.zjun.demo.experimentaldemo.block_test

import android.graphics.Color
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.base.BaseActivity
import com.zjun.demo.experimentaldemo.databinding.ActivityBlockTestBinding

/**
 * 代码块测试
 */
class BlockTestActivity : BaseActivity<ActivityBlockTestBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_block_test

    override fun initialize() {

    }

    /**
     * addView
     */
    private fun addViewByIndex(targetView: View?, addViewProxy:(Int)->Unit){
        if(targetView == null){
            addViewProxy.invoke(-1)
        }else{
            val index = mBinding.testLayout.indexOfChild(targetView)
            if(index != -1){
                addViewProxy.invoke(index)
            }else{
                addViewProxy.invoke(-1)
            }
        }
    }

    fun addView(view: View) {
        addViewByIndex(findViewById(R.id.webrtc)){
            val mLoadingTextView = TextView(this)
            mLoadingTextView.text = "位置 = ${mBinding.testLayout.childCount}"
            mLoadingTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
            mLoadingTextView.setTextColor(Color.BLUE)
            mBinding.testLayout.addView(mLoadingTextView,it)
        }
    }

}